package com.softeem.music_sys.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.softeem.music_sys.comm.Result;
import com.softeem.music_sys.entity.Collection;
import com.softeem.music_sys.service.CollectionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "收藏接口", description = "收藏相关操作：增删改查等")
@RestController
@RequestMapping("/collection")
public class CollectionController {

    @Autowired
    private CollectionService collectionService;

    // 添加收藏
    @Operation(summary = "添加收藏", description = "添加收藏，提供JSON格式收藏数据")
    @PostMapping
    public Result add(@RequestBody Collection collection) {
        // 检查是否已收藏
        Collection existingCollection = collectionService.getOne(
                new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<Collection>()
                        .eq("user_id", collection.getUserId())
                        .eq("target_id", collection.getTargetId())
                        .eq("type", collection.getType())
        );
        if (existingCollection != null) {
            return Result.fail("已收藏");
        }
        boolean flag = collectionService.save(collection);
        return flag ? Result.success().setMsg("收藏成功").setData(collection) : Result.fail("收藏失败");
    }

    // 取消收藏
    @Operation(summary = "取消收藏", description = "根据收藏ID取消收藏")
    @Parameters({
            @Parameter(name = "id", description = "收藏ID")
    })
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        // 检查收藏是否存在
        Collection collection = collectionService.getById(id);
        if (collection == null) {
            return Result.fail("收藏记录不存在");
        }
        boolean flag = collectionService.removeById(id);
        return flag ? Result.success().setMsg("取消收藏成功") : Result.fail("取消收藏失败");
    }

    // 收藏列表查询（分页）
    @Operation(summary = "收藏列表查询", description = "收藏列表查询，支持分页")
    @Parameters({
            @Parameter(name = "page", description = "页码"),
            @Parameter(name = "limit", description = "每页数量")
    })
    @GetMapping("/list")
    public Result list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit
    ) {
        Page<Collection> pageUtil = new Page<>(page, limit);
        List<Collection> list = collectionService.list(pageUtil);
        return Result.success().setData(list);
    }

    // 根据ID获取收藏信息
    @Operation(summary = "获取收藏信息", description = "根据收藏ID获取收藏详细信息")
    @Parameters({
            @Parameter(name = "id", description = "收藏ID")
    })
    @GetMapping("/{id}")
    public Result getCollectionById(@PathVariable Integer id) {
        Collection collection = collectionService.getById(id);
        if (collection == null) {
            return Result.fail("收藏记录不存在");
        }
        return Result.success().setData(collection);
    }

    // 根据用户ID查询收藏列表
    @Operation(summary = "根据用户ID查询收藏", description = "根据用户ID查询该用户的所有收藏")
    @Parameters({
            @Parameter(name = "userId", description = "用户ID")
    })
    @GetMapping("/byUser/{userId}")
    public Result getCollectionByUserId(@PathVariable Integer userId) {
        List<Collection> list = collectionService.list(new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<Collection>().eq("user_id", userId));
        return Result.success().setData(list);
    }

    // 根据收藏对象类型和ID查询收藏列表
    @Operation(summary = "根据对象查询收藏", description = "根据收藏对象类型和ID查询收藏列表")
    @Parameters({
            @Parameter(name = "type", description = "收藏对象类型"),
            @Parameter(name = "targetId", description = "收藏对象ID")
    })
    @GetMapping("/byTarget")
    public Result getCollectionByTarget(
            @RequestParam String type,
            @RequestParam Integer targetId
    ) {
        List<Collection> list = collectionService.list(
                new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<Collection>()
                        .eq("type", type)
                        .eq("target_id", targetId)
        );
        return Result.success().setData(list);
    }
}